//
// Created by user on 2022/3/22.
//

#include <vector>
#include <queue>
using namespace std;
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
//层序遍历， 队列
int minDepth(TreeNode* root) {
    if (!root)return 0;
    queue<TreeNode*> qe;
    qe.push(root);
    int levels = 0;
    while (!qe.empty()){
        int size = qe.size();
        levels++;
        for (int i = 0; i < size; ++i) {
            TreeNode *currNode = qe.front();
            qe.pop();
            if(!currNode->left && !currNode->right){
                return levels;
            }
            if(currNode->left){qe.push(currNode->left);}
            if(currNode->right){qe.push(currNode->right);}
        }
    }
    return levels;
}